package com.one.rope.mvp.basecore.util;

import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * 签名签证方法支持
 */
public class SecurityUtils {

  private static final String ENCODING = "UTF-8";

  /**
   * HmacSHA1
   */
  public static String HmacSHA1Encrypt(String encryptText, String encryptKey) throws Exception {
    return encrypt(encryptText, encryptKey, "HmacSHA1");
  }

  private static String encrypt(String encryptText, String encryptKey, String secretType)
      throws Exception {
    byte[] data = encryptKey.getBytes(ENCODING);
    SecretKey secretKey = new SecretKeySpec(data, secretType);
    Mac mac = Mac.getInstance(secretType);
    mac.init(secretKey);

    byte[] text = encryptText.getBytes(ENCODING);
    byte[] bytes = mac.doFinal(text);
    return byteToString(bytes);
  }

  /**
   * byte转换为String
   *
   * @param bytes byte[] 数组
   * @return String
   */
  public static String byteToString(byte[] bytes) {
    StringBuffer hexString = new StringBuffer();
    for (int i = 0; i < bytes.length; i++) {
      String shaHex = Integer.toHexString(bytes[i] & 0xFF);
      if (shaHex.length() < 2) {
        hexString.append(0);
      }
      hexString.append(shaHex);
    }
    return hexString.toString();
  }

  public static String HmacSHA256Encrypt(String encryptText, String encryptKey) throws Exception {
    return encrypt(encryptText, encryptKey, "HmacSHA256");
  }

  public static String HmacSHA512Encrypt(String encryptText, String encryptKey) throws Exception {
    return encrypt(encryptText, encryptKey, "HmacSHA512");
  }

  /**
   * MD5加密码32位
   */
  public static String Md5(String data) throws Exception {
    MessageDigest md5 = null;
    md5 = MessageDigest.getInstance("MD5");
    char[] charArray = data.toCharArray();
    byte[] byteArray = new byte[charArray.length];
    for (int i = 0; i < charArray.length; i++) {
      byteArray[i] = (byte) charArray[i];
    }
    byte[] md5Bytes = md5.digest(byteArray);
    StringBuffer hexValue = new StringBuffer();
    for (int i = 0; i < md5Bytes.length; i++) {
      int val = ((int) md5Bytes[i]) & 0xff;
      if (val < 16) {
        hexValue.append("0");
      }
      hexValue.append(Integer.toHexString(val));
    }
    return hexValue.toString();
  }

  /**
   * Md5加密长度为16
   *
   * @param data String
   * @return String
   */
  public static String Md5Len16(String data) throws Exception {
    String md5String = Md5(data);
    if (StringUtils.isNullOrEmpty(md5String)) {
      return md5String;
    }
    return md5String.substring(8, 24);
  }

  public static String sha1(String value)
      throws NoSuchAlgorithmException, UnsupportedEncodingException {
    return encode(value, "SHA-1");
  }

  public static String sha256(String value)
      throws NoSuchAlgorithmException, UnsupportedEncodingException {
    return encode(value, "SHA-256");
  }

  public static String encode(String value, String encryptType)
      throws NoSuchAlgorithmException, UnsupportedEncodingException {
    // try {
    MessageDigest md = MessageDigest.getInstance(encryptType);
    md.reset();
    md.update(value.getBytes("UTF-8"));
    byte[] actual = md.digest();
    int actual_len = actual.length;
    StringBuilder actual_hex = new StringBuilder(actual.length * 2);
    for (int i = 0; i < actual_len; i++) {
      actual_hex.append(String.format("%02X", actual[i]));
    }
    return actual_hex.toString();
  }

}
